package com.kktestspb.demo.domain;


import com.kktestspb.demo.polo.Product;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.helper.StringUtil;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;


@Component
public class ScrapDomain {

    /**
     * 此处测试爬取天气预报
     */
    public String  getTest1(){
        //SH7天天气预报
        String url="http://www.weather.com.cn/weather/101020100.shtml";
        StringBuffer sb =new StringBuffer();
        try {

            Document document= Jsoup.connect(url).
                    timeout(4000).get();
            Elements elements=document.getElementsByClass("t clearfix");
            for(Element ele:elements){
                String sss=ele.text();
                List<Node> schild=ele.childNodes();
                for(Node node:schild){
                    if(!StringUtil.isBlank(node.toString().trim())){
                        List<Node> nodechild=node.childNodes();
                        for(Node no:nodechild){
                            if(!StringUtil.isBlank(no.toString())){
                                List<Node>  li= no.childNodes();
                                for(Node nn:li){
                                    if(!StringUtil.isBlank(nn.toString())){
                                        //System.out.println(nn);
                                        // System.out.println(li.toString());
                                        sb.append(nn.toString());

                                    }

                                }


                            }
                        }
                        System.out.println("------------");

                    }


                }

                //   System.out.println(sss);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sb.toString();
    }

    /**
     * 爬取天猫欧诗漫产品销量
     * @return
     */
    public List<Product> getTest2(){
   // public static List<Product> getTest2() {
        List<Product> products=new ArrayList<>();
        StringBuffer sb =new StringBuffer();
        String input = "欧诗漫";
        // 需要爬取商品信息的网站地址
        String url = "https://list.tmall.com/search_product.htm?q=" + input;
        // 动态模拟请求数据
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(url);
        // 模拟浏览器浏览（user-agent的值可以通过浏览器浏览，查看发出请求的头文件获取）
        httpGet.setHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36");
        CloseableHttpResponse response = null;
        try {
            response = httpclient.execute(httpGet);
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 获取响应状态码
        int statusCode = response.getStatusLine().getStatusCode();
        try {
            HttpEntity entity=response.getEntity();
            // 如果状态响应码为200，则获取html实体内容或者json文件
            if(statusCode == 200){
                String html = null;
                try {
                    html = EntityUtils.toString(entity, Consts.UTF_8);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                // 提取HTML得到商品信息结果
                Document doc = null;
                // doc获取整个页面的所有数据
                doc = Jsoup.parse(html);
                //输出doc可以看到所获取到的页面源代码
                //   System.out.println(doc);
                // 通过浏览器查看商品页面的源代码，找到信息所在的div标签，再对其进行一步一步地解析
                Elements ulList = doc.select("div[class='view grid-nosku view-noCom']");
                Elements liList = ulList.select("div[class='product  ']");
                // 循环liList的数据（具体获取的数据值还得看doc的页面源代码来获取，可能稍有变动）

                for (Element item : liList) {
                    Product product=new Product();
                    // 商品ID
                    String id = item.select("div[class='product  ']").select("p[class='productStatus']").select("span[class='ww-light ww-small m_wangwang J_WangWang']").attr("data-item");
                    product.setProId(id);
                    // 商品名称
                    String name = item.select("p[class='productTitle']").select("a").attr("title");
                    product.setProName(name);
                    // 商品价格
                    String price = item.select("p[class='productPrice']").select("em").attr("title");
                    product.setProPrice(price);
                    // 商品成交量
                    String amout = item.select("p[class='productStatus']").select("em").text();
                    Double damout=0.0;
                    String longamt="" ;
                    if(amout.contains("万笔")){
                        longamt =amout.substring(0,amout.indexOf("万笔"));
                        damout=Double.parseDouble(longamt)*10000;
                    }
                    else{
                        longamt =amout.substring(0,amout.indexOf("笔"));
                        damout=Double.parseDouble(longamt);
                    }
                    product.setProMonthAmout(damout);
                    // 商品店铺名称
                    String prodaddress = item.select("p[class='productStatus']").select("span[class='ww-light ww-small m_wangwang J_WangWang']").attr("data-tnick");
                    product.setProShopName(prodaddress);
                    // 商品网址
                    String goodsUrl = item.select("p[class='productTitle']").select("a").attr("href");
                    product.setProNetAddress(goodsUrl);
                    products.add(product);
                }

                // 消耗掉实体
                try {
                    EntityUtils.consume(response.getEntity());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else {
                // 消耗掉实体
                try {
                    EntityUtils.consume(response.getEntity());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } finally {
            try {
                response.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return products;
    }

   /* public static void main(String[] args) {
        List<Product> sli=getTest2();
        for(Product p:sli){
            System.out.println(p);
        }*/

  // }
}
